/**
 * modified by kuangcc on 2018/6/24.
 */
  //极光推送初始化
  //用于推送的变量。
  var push;
  /* 极光推送的初始化方法 **/
export function setBadge(badge) {
    if (push) {
      console.log('--- JPush Set Badge ---', badge);
      plugins.jPushPlugin.setBadge(badge);
    }
  }

export function check() {
    if (window.jpush && push) {
      plugins.jPushPlugin.receiveNotificationIniOSCallback(window.jpush);
      window.jpush = null;
    }
  }
export function getPushMessageListBadgeSetting(data){
    var redPoint;
    try {
      data = JSON.stringify(data);
      console.log(data);
      var tmpMobile = localStorage["userName"];
      console.log("tmpMobile:" + tmpMobile);
      if (tmpMobile) {



        $.ajax({
          type: "POST",
          url: apiService + 'sysPush/getPushMessageListBadgeSetting',
          data: {
            userName: tmpMobile,
          },
          success: function (res) {
            if(res && res.state == 0){
              var badgeNum = 0;
              if(res.result){
                for(var x in res.result){
                  if(res.result[x].isRead == 0){
                    badgeNum = badgeNum + 1;
                  }
                }
                if(badgeNum > 0){
                  console.log("显示消息提示微章");
                  redPoint = true;
                  console.log("App应用角标数量：" +badgeNum);
                  window.plugins.jPushPlugin.setBadge(badgeNum);
                  window.plugins.jPushPlugin.setApplicationIconBadgeNumber(badgeNum);
                }else{
                  console.log("不显示消息提示微章");
                  redPoint = false;
                  console.log("App应用角标数量：" +badgeNum);
                  window.plugins.jPushPlugin.setBadge(badgeNum);
                  window.plugins.jPushPlugin.setApplicationIconBadgeNumber(badgeNum);
                }
              }else{
                console.log("不显示消息提示微章");
                redPoint = false;
                console.log("App应用角标数量：" + badgeNum);
                window.plugins.jPushPlugin.setBadge(badgeNum);
                window.plugins.jPushPlugin.setApplicationIconBadgeNumber(badgeNum);
              }
            }else{
              console.log("获取数据失败！");
            }
          }
        })
        //广播机制
        //console.log("tmpMobile:  "+tmpMobile);
      } else {
        //console.log("tmpMobile:  "+tmpMobile);
      }
    } catch (exception) {
      console.log(exception)
    }
  }

  /* 极光推送的初始化方法 **/
export  function init() {
    push = window.plugins && window.plugins.jPushPlugin;
    if (push) {
      console.log("判断成功，进入初始化");
      plugins.jPushPlugin.init();
      //开启通知的自定义样式
      plugins.jPushPlugin.setCustomPushNotificationBuilder();
      //调试模式
      plugins.jPushPlugin.setDebugMode(true);
      //推送状态
    /*  var pushState = localStorage["pushState"];
      if (pushState == "0") {*/
        window.plugins.jPushPlugin.resumePush();
      /*  console.log("pushState: " + pushState);
     } else {
        window.plugins.jPushPlugin.stopPush();
        console.log("pushState: " + pushState);
      }*/

      window.plugins.jPushPlugin.getUserNotificationSettings(function (result) {
        if (result == 0) {
          console.log("系统设置中关闭了应用推送。");
        } else if (result > 0) {
          // 系统设置中打开了应用推送。
          console.log("系统设置中打开了应用推送。");
        }
      });
      /* 安卓系统接收到通知的回调函数 **/
      plugins.jPushPlugin.receiveNotificationInAndroidCallback = function (data) {
        getPushMessageListBadgeSetting(data);
      /*  try {
          data = JSON.stringify(data);
          console.log('--- Receive Android Notification --- ' + data);
          var tmpMobile = localStorage["userName"];
          if (tmpMobile) {
            $.ajax({
              type: "POST",
              url: apiService + 'sysPush/getPushMessageListBadgeSetting',
              data: {
                userName: tmpMobile,
              },
              success: function (res) {
                console.log("执行回调函数中。。。。。。");
                console.log("res中result的值" + res.result.name);
              }
            })
            //console.log("tmpMobile:  "+tmpMobile);
          } else {
            console.log("执行回调函数失败。。。。。。");
            //console.log("tmpMobile:  "+tmpMobile);
          }
        } catch (exception) {
          console.log(exception)
        }*/
      };
      /* 安卓系统打开通知的回调函数 **/
      plugins.jPushPlugin.openNotificationInAndroidCallback = function (data) {
        console.log('--- Open Android Notification --- ' + data);
        this.$router.push({
          path: '/my/page/jpushList',
          query: {}
        })
      };
      /* 苹果系统接收到通知的回调函数 **/
      document.addEventListener("jpush.receiveNotification", function (data) {
        console.log('--- Receive Ios Notification --- ');
        getPushMessageListBadgeSetting(data);
       /* try {
          data = JSON.stringify(data);
          console.log(data);
          var tmpMobile = localStorage["userName"];
          console.log("tmpMobile:" + tmpMobile);
          if (tmpMobile) {
            $.ajax({
              type: "POST",
              url: apiService + 'sysPush/getPushMessageListBadgeSetting',
              data: {
                userName: tmpMobile,
              },
              success: function (res) {
                if(res && res.state == 0){
                  var badgeNum = 0;
                  if(res.result){
                    for(x in res.result){
                      if(x.isRead == 0){
                        badgeNum = badgeNum + 1;
                      }
                    }
                    if(badgeNum > 0){
                      console.log("显示消息提示微章");
                      redPoint = true;
                      console.log("App应用角标数量：" +badgeNum);
                        window.plugins.jPushPlugin.setBadge(badgeNum);
                        window.plugins.jPushPlugin.setApplicationIconBadgeNumber(badgeNum);
                    }else{
                      console.log("不显示消息提示微章");
                      redPoint = false;
                      console.log("App应用角标数量：" +badgeNum);
                        window.plugins.jPushPlugin.setBadge(badgeNum);
                        window.plugins.jPushPlugin.setApplicationIconBadgeNumber(badgeNum);
                    }
                  }else{
                    console.log("不显示消息提示微章");
                    redPoint = false;
                    console.log("App应用角标数量：" + badgeNum);
                    window.plugins.jPushPlugin.setBadge(badgeNum);
                    window.plugins.jPushPlugin.setApplicationIconBadgeNumber(badgeNum);
                  }
                }else{
                  console.log("获取数据失败！");
                }
              }
            })
            //广播机制
            //console.log("tmpMobile:  "+tmpMobile);
          } else {
            //console.log("tmpMobile:  "+tmpMobile);
          }
        } catch (exception) {
          console.log(exception)
        }*/
      }, false);

      /* 苹果系统打开收到通知的回调函数 **/
      document.addEventListener("jpush.openNotification", function (data) {
        console.log('--- Open Ios Notification ---');
  /*      var notification =JSON.parse(data);
        //app是否运行
        var isActive = notification.notification;*/
        //广播机制
        window.location.href = "#ajax/myInfo/msg-index.html";
        //$state.go("tab.msg");
        // $ionicViewSwitcher.nextDirection("forward");
      }, false);
    }
  }
  //设置别名
export  function setAlias(alias) {
    if (push) {
      console.log('--- JPush Set Alias ---', alias);
      plugins.jPushPlugin.setAlias(alias,function (e) {
        console.log(e);
        console.log("别名设置成功");
      },function (v) {
        console.log(v);
        console.log("别名设置失败");
      });
    }
  }



